package test {

  object Test2 {

    def collatz(a: Array[Int]): Array[Int] = {
      if (a.length == 0) {
        println("Array leer!");
        a
      } else if (a(a.length - 1) == 1) {
        println(a.deep)
        a
      } else {
        if (a(a.length - 1) % 2 == 0) {
          val arr = new Array[Int](a.length + 1)
          a copyToArray (arr)
          arr(arr.length - 1) = a(a.length - 1) / 2
          collatz(arr)
        } else {
          val arr = new Array[Int](a.length + 1)
          a copyToArray (arr)
          arr(arr.length - 1) = a(a.length - 1) * 3 + 1
          collatz(arr)
        }
      }
    }

    def collatz2(a: Array[Int]): Array[Int] = {
      if (a.length == 0) {
        println("Array leer!")
        a
      } else if (a(a.length - 1) == 1) {
        println(a.deep)
        a
      } else if (a(a.length - 1) % 2 == 0) collatz2(a :+ a(a.length - 1) / 2)
      else collatz(a :+ a(a.length - 1) * 3 + 1)
    }

    object Time {
      def apply[T](name: String)(block: => T) {
        val start = System.currentTimeMillis
        try {
          block
        } finally {
          val diff = System.currentTimeMillis - start
          println("# Block \"" + name + "\" completed, time taken: " + diff + " ms (" + diff / 1000.0 + " s)")
        }
      }
    }

    def main(args: Array[String]): Unit = {
      println("----------------------")
//      Time("Collatz mit :+"){
//    	  collatz2(Array(2167))
//      }
      Time("Collatz1 mit Copy"){
        collatz(Array(2167))
      }
    }
  }
}